/*
 * Copyright (c) 2017, Iotelligent Corporation
 * All rights reserved.
 *
 * Description:
 *
 *   iBAT2000 UART global typedefs, macros and function prototype.
 */

#ifndef __IOT_HW_UART_H__
#define __IOT_HW_UART_H__

#include "stm32f4xx.h"
#include "iot_hw_types.h"
#include "iot_errno.h"
#define UART_RX_FIFO_LEN        32
#define UART_TX_FIFO_LEN        32

#define UART_RX_BUFF_LEN_MAX    1024
#define UART_TX_BUFF_LEN_MAX    1024

enum
{
    UART_INT_TX = 0,
    UART_INT_RX
};

typedef struct
{
    uint8_t  recv_buf[UART_RX_BUFF_LEN_MAX];
    uint32_t recv_drop; // droped char number due to buffer full
    uint32_t recv_wptr;
    uint32_t recv_rptr;

    uint8_t  send_buf[UART_TX_BUFF_LEN_MAX];
    uint32_t send_wptr;
    uint32_t send_rptr;
} UART_RX_TX_BUFF_T;

typedef struct
{
    USART_TypeDef *UARTx_Num;
//		GPIO_InitTypeDef GPIO_InitStructureT;
//		GPIO_TypeDef *GPIOxT;
//	
//		GPIO_InitTypeDef GPIO_InitStructureR;
//		GPIO_TypeDef *GPIOxR;

	
    USART_InitTypeDef UARTx_InitStruct;
    uint8_t  UARTx_IntEnable;
    uint16_t UARTx_InitType;
    NVIC_InitTypeDef UARTx_Nvic;
} IOT_HW_UART_CFG_T;

extern FUN_INT_CALLBACK_T uart0_rx_int_callback;
extern FUN_INT_CALLBACK_T uart0_tx_int_callback;
extern FUN_INT_CALLBACK_T uart1_rx_int_callback;
extern FUN_INT_CALLBACK_T uart1_tx_int_callback;


void uart_init( void );
void uart_send_char( USART_TypeDef *UARTx, int ch );
int uart_get_char( USART_TypeDef *UARTx );
int uart_write_buf( USART_TypeDef *UARTx, uint8_t *p_buf, uint32_t len );
int uart_read_buf( USART_TypeDef *UARTx, uint8_t *p_buf, uint32_t len );
int uart_read_buf_one_char( USART_TypeDef *UARTx, uint8_t *ch );
int uart_check_send_complete( USART_TypeDef *UARTx );

#endif /* __IOT_HW_UART_H__ */
